<?php

	/**
	 * @see Bike_Model_Mapper_Abstract
	 */
	require_once 'Bike/Model/Mapper/Abstract.php';
	
	/**
	 * @see Bike_Model_Mapper_Dependence
	 */
	require_once 'Bike/Model/Mapper/Dependence.php';
	
	
	class Bike_Model_Mapper_UserAuth extends Bike_Model_Mapper_Abstract
	{
		const DB_TABLE_NAME = 'user_auth';
		const DB_TABLE_PK_NAME = 'user_id';
		const DB_TABLE_ALIAS = 'ua';

		static public function getInstance()
	    {
	        return self::_getInstance(__CLASS__);
	    }

	    protected function _getDependences()
	    {
	    	return array(
	    		$this->createDependence(array(
					'type' => Bike_Model_Mapper_Dependence::JOIN_LEFT,
					'name' => 'area',
					'mapperClass' => 'Bike_Model_Mapper_Area',
					'parentJoinFields' => array('area_id'),
				)),
	    	);
	    }
	    
	    public function getUserIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('User ID'),
		    		'order' => 100,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getAreaIdFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Area'),
		    		'order' => 110,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'type' => 'select',
		    		'required' => true,
			    	'multiOptions' => Bike_Model_Mapper_Area::getInstance()->fetchAll()->getMultiOptions('area_id', 'code'),
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'dataIndex' => 'area_code',
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getLoginFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Login'),
		    		'order' => 120,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'description' => $this->translate('Login for authentication. Letters, digits, "_" and "." ONLY!'),
					'required' => true,
					'validators' => array(
						array('StringLength', False, array('5', '15')),
						array('Regex', False, array('pattern' => '/^[\w\d\_\.]+$/')),
					),
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getPasswordFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
		    		'label' => $this->translate('Password'),
		    		'order' => 130,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
		    		'required' => true,
		    		'validators' => array(
						array('StringLength', False, array('6', '15')),
					),
	    		);
	    		case self::PARAMS_FORM_ELEMENT_UPDATE: return array(
		    		'required' => false,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    public function getPasswordDFieldParams($type = null)
	    {
	    	switch($type)
	    	{
	    		case self::PARAMS_COMMON: return array(
	    			'label' => $this->translate('Repeat password'),
	    			'order' => 140,
	    		);
	    		case self::PARAMS_LIST_COLUMN: return array(
	    			'notUsed' => true,
	    		);
	    		case self::PARAMS_LIST_MODEL: return array(
	    			'notUsed' => true,
	    		);
	    		case self::PARAMS_FORM_ELEMENT: return array(
	    			'required' => true,
	    		);
	    		case self::PARAMS_FORM_ELEMENT_UPDATE: return array(
	    			'required' => false,
	    		);
	    		default: return array();
	    	}
	    }
	    
	    
	    public function getPasswordBeforeUpdateHandler(Bike_Model_Abstract $model)
	    {
			if(!$model->isNew() && !$model->getPassword())
			{
				$model->setPassword($model->getCleanPassword());
			}
			else
			{
	    		$model->setPassword(md5($model->getPassword()));
			}	    	
	    }
	    
	    public function getAfterAddFormElementsHandler(Bike_Form $form, array $params = null, $fieldNamePrefix = '')
	    {
	    	parent::getAfterAddFormElementsHandler($form, $params, $fieldNamePrefix);
	    	
	    	$password2 = $fieldNamePrefix.'password_d';
	    	$password = $fieldNamePrefix.'password';
	    	$form->addElement('password', $password2, array_merge(array(
	    		'validators' => array(
	    			array('Identical', false, $password),
	    		),
	    	), (isset($params[$password2])) ? $params[$password2] : array()));
	    	
	    	$form->$password->setValue('');
	    }
	    
	    public function getTaskUpdateDirtyDataHandler(Bike_Form $form, StdClass $dirtyData, $fieldNamePrefix = '')
	    {
	    	parent::getTaskUpdateDirtyDataHandler($form, $dirtyData, $fieldNamePrefix);
	    
	    	$fieldName = $fieldNamePrefix.'password_d';
	    	unset($dirtyData->$fieldName);
	    }
	}
